package com.dhcc.sdc.api.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dhcc.cdc.busi.dao.CdcDiagnosisMapper;
import com.dhcc.cdc.busi.dao.CdcEpisodeMapper;
import com.dhcc.cdc.busi.dao.CdcExpenseMapper;
import com.dhcc.cdc.busi.entity.CdcDiagnosisEntity;
import com.dhcc.cdc.busi.entity.CdcEpisodeEntity;
import com.dhcc.cdc.busi.entity.CdcExpenseEntity;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sdc.api.entity.DRGParams;
import com.dhcc.sdc.api.service.ISdsDrgParamsService;
import com.dhcc.sdc.ws.SdsGetFeeService.SdsGetFeeServiceLocator;
import com.dhcc.sdc.ws.SdsGetFeeService.SdsGetFeeServiceSoap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("sdsDrgParamsService")
public class SdsDrgParamsServiceImpl implements ISdsDrgParamsService {
    @Autowired
    private CdcEpisodeMapper episodeMapper;
    @Autowired
    private CdcDiagnosisMapper diagnosisMapper;
    @Autowired
    private CdcExpenseMapper expenseMapper;

    @Value("${sys.dhcHisCacheIp}")
    private String dhcHisCacheIp;  //获取his发布的接口地址 127.0.0.1
    @Value("${sys.dhcHisCachePath}")
    private String dhcHisCachePath; //获取his接口路径  imedical/web   dthealth/web

    @Override
    public String combineParamStr(DRGParams drgParams) {
        return "{" +
                "    \"ACCTUAL_DAYS\": 50," +
                "    \"AGE\": "+ drgParams.getAge() +"," +
                "    \"CASE_ID\": \""+ drgParams.getXcode() +"\"," +
                "    \"DIAGS_CODE\": "+ drgParams.getDiaList() +"," +
                "    \"DISEASE_CODE\": \""+ drgParams.getMainDiagnosis() +"\"," +
                "    \"DISEASE_NAME\": \""+ drgParams.getMainDiagnosisDesc() +"\"," +
                "    \"GENDER\": \""+ drgParams.getSex() +"\"," +
                "    \"OPERS_CODE\": []," +
                "    \"PAY_TYPE\": \"1\"," +
                "    \"SF0100\": -1," +
                "    \"SF0102\": -1," +
                "    \"SF0102_B\": -1," +
                "    \"SF0104\": \"\"," +
                "    \"SF0108\": 1," +
                "    \"TOTAL_EXPENSE\": "+ drgParams.getTotalExpense() + "," +
                "}";
    }

    @Override
    public DRGParams getDRGParams(CdcEpisodeEntity episodeEntity) {
        DRGParams drgParams = DRGParams.getInstance(episodeEntity.getXcode());
        Map<String, Object> map = new HashMap<>();
        map.put("episodeId",episodeEntity.getId());
        Float feeTotal = (float) 0;
        try {
            SdsGetFeeServiceLocator dhcLocator = new SdsGetFeeServiceLocator();
            SdsGetFeeServiceSoap soap = dhcLocator.getSdsGetFeeServiceSoap(dhcHisCacheIp, dhcHisCachePath);
            String backInfo = soap.getAdmFee(episodeEntity.getXcode());
            JSONArray feeObject = JSONArray.parseArray(backInfo);
            JSONObject s = JSONObject.parseObject(feeObject.get(0).toString());
            feeTotal = s.getFloat("fee");
            if (CommonUtil.isEmpty(feeTotal)){
                feeTotal = (float) 0;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        List<CdcDiagnosisEntity> diagnosisEntities = diagnosisMapper.list(map);
        String diaListString = "[";
        if (diagnosisEntities.size()>0){
            for (CdcDiagnosisEntity diagnosisEntity : diagnosisEntities) {
                if (diagnosisEntity.getIsMain() == 1){
                    drgParams.setMainDiagnosis(diagnosisEntity.getIcd10());
                    drgParams.setMainDiagnosisDesc(diagnosisEntity.getDescription());
                }else {
                    diaListString += "{\"CODE\": \""+ diagnosisEntity.getIcd10() +
                            "\", \"DESC\": \""+diagnosisEntity.getDescription() +"\"},";
                }
            }
            if (diaListString.lastIndexOf(",") != -1){
                diaListString = diaListString.substring(0,diaListString.lastIndexOf(",")).concat("]");
            }else {
                diaListString += "]";
            }
        }else {
            drgParams.setMainDiagnosis("");
            drgParams.setMainDiagnosisDesc("");
            diaListString += "]";
        }
        drgParams.setEpisodeId(episodeEntity.getId());
        drgParams.setAge(episodeEntity.getAge());
        if (CommonUtil.equals("男",episodeEntity.getGender())){
            drgParams.setSex("1");
        }else if (CommonUtil.equals("女",episodeEntity.getGender())){
            drgParams.setSex("2");
        }else if (StringUtils.isEmpty(episodeEntity.getGender())){
            drgParams.setSex("9");
        }
        drgParams.setDiaList(diaListString);
        drgParams.setTotalExpense(feeTotal.toString());
        return drgParams;
    }
}
